﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _8583BitmapViewer
{
    public partial class Form1 : Form
    {
        //private System.Windows.Forms.Button[] m_btnArray;
        //private Test.ButtonX[] m_btnArray;
        private System.Windows.Forms.Button[] m_btnArray;
        private int m_iBitMapSize = 128;
        private BitArrayIndexer.BitArray m_arrBitArray;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            m_btnArray = new System.Windows.Forms.Button[m_iBitMapSize];
            //m_btnArray = new Test.ButtonX[m_iBitMapSize];
            for (int i = 0; i < 128;i++ )
            {

                //m_btnArray[i] = new System.Windows.Forms.Button();
                if (i<16)
                {
                    m_btnArray[i] = new System.Windows.Forms.Button();
                }
                else
                {
                    m_btnArray[i] = new Test.ButtonX();
                }
                int x=0;
                int y=0;
                if (i>0)
                {
                    x = i % 16;
                    y = i >> 4;
                }

                m_btnArray[i].Location = new System.Drawing.Point(x*40+(x>7?25:15),y*40+7);
                m_btnArray[i].Size = new System.Drawing.Size(36, 36);
                m_btnArray[i].BackColor = Color.Green;
                m_btnArray[i].Text = (i+1).ToString();
                this.Controls.Add(m_btnArray[i]);
                this.m_btnArray[i].Click += new System.EventHandler(this.btnArray_Click);
            }
            //
            m_arrBitArray = new BitArrayIndexer.BitArray(m_iBitMapSize);
        }
        private void btnArray_Click(object sender, EventArgs e)
        {
            string ss = ((System.Windows.Forms.Button)sender).Text.ToString();
            int idx = 1;
            try
            {
                idx = int.Parse(ss);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            m_arrBitArray[m_iBitMapSize-idx] = !m_arrBitArray[m_iBitMapSize-idx];
            UpdateBitmap();
        }
        private void button_UpdateBitmap_Click(object sender, EventArgs e)
        {
            if (textBox_BitmapString.Text.Length>0)
            {//更新文本框中的内容到ButtonArray
                GetTextAndSaveToBitArray();
                UpdateBitmap();
            }
            else
            {
                textBox_BitmapString.Text = m_arrBitArray.GetHexString();
                UpdateBitmap();
            }
        }
        private void GetTextAndSaveToBitArray()
        {
            string str=textBox_BitmapString.Text;
            str=RemoveNotNumber(str);
            if ((str.Length>0)&&(str.Length!=16 && str.Length!=32))
            {
                MessageBox.Show("字符串长度不对，长度为:"+ str.Length.ToString());
            }
            string ss = str.PadRight(32,'0');
            if (str.Length>32)
            {
                ss = str.Substring(0, 32);
            }
            else
            {
                ss = str.PadRight(32, '0');
            }
            m_arrBitArray.ResetHexStringData(ss);
        }
        /// <summary>
        /// 去掉字符串中的非数字
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string RemoveNotNumber(string key)
        {
            //return System.Text.RegularExpressions.Regex.Replace(key, @"[^\d]*", "");
            //string sss = System.Text.RegularExpressions.Regex.Replace(key, @"[^0-9a-fA-F]*", "");
            return System.Text.RegularExpressions.Regex.Replace(key, @"[^0-9a-fA-F]*", "");
        }
        /// <summary>
        /// 把Bit数组中的内容更新到Button数组进行显示
        /// </summary>
        private void UpdateBitmap()
        {
            textBox_BitmapString.Text = m_arrBitArray.GetHexString();

            for (int i = 0; i < m_iBitMapSize;i++ )
            {
                if (m_arrBitArray[i])
                {
                    //m_btnArray[m_iBitMapSize - i - 1].BackColor = Color.Transparent;
                    m_btnArray[m_iBitMapSize-i-1].BackColor=Color.Red;
                }
                else
                {
                    //m_btnArray[m_iBitMapSize - i - 1].BackColor = Color.Transparent;
                    m_btnArray[m_iBitMapSize-i-1].BackColor = Color.Green;
                }
            }
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData==Keys.Escape)
            {
                Application.Exit();
            }
        }

        private void button_Copy_Click(object sender, EventArgs e)
        {
            string str = textBox_BitmapString.Text;
            if (str!="")
            {
                str = System.Text.RegularExpressions.Regex.Replace(str,@"[^0-9a-fA-F ]*", "");
                Clipboard.SetDataObject(str);
                MessageBox.Show("如下内容已复制到剪切板:\r\n" + str);
            }
        }
    }
}
